unit dGrids;

interface
 uses DBGrids,
      Grids,
      Windows,
      Graphics,
      SysUtils,
      DB;

  type

       TStyle = (gsNormal, gsCustom, gsPriceList, gsMSMoney, gsBrick,
              gsDesert, gsEggplant, gsLilac, gsMaple, gsMarine,
              gsRose, gsSpruce, gsWheat, gsSoftWheat, gsSoftRose,
              gsAquaBlue, gsSoftMaple, gsSoftLilac, gsSoftDesert, gsSoftEggPlant,
              gsSoftBrick,  gsSoftSpruce, gsSoftYellowGreen, gsSoftGray);


  TGradientColor = record
  InitColor : TColor;
  FinColor : TColor;
  end;

  TGridColor = record
   GridStyle : TStyle;
   OddColor  : TColor;
   EvenColor : TColor;
  end;

  TUserStyle = record
   GridColor : TGridColor;
   Gradient : TGradientColor;
  end;

   TMyDBGrid = class(TDBGrid)
   public
   function CenterCell( Column: TColumn ): Integer;
   property DefaultRowHeight;
   property row;
   property col;
   published
   end;

   procedure GridCheckBox(ASender: TDBGrid; const Rect: TRect; DataCol: Integer; Column: TColumn;
   State: TGridDrawState);
   procedure GridZebrado(Sender: TObject;
                      const Rect: TRect;
                      DataCol: Integer;
                      Column: TColumn;
                      State: TGridDrawState;
                      UserStyle  : TUserStyle);

   const
    DefaultRowHeight = 25;

    var
      FUserStyle  : TUserStyle;

implementation

{ TMyDBGrid }

function TMyDBGrid.CenterCell( Column: TColumn ): Integer;
begin
 result   := DefaultRowHeight;
 if Assigned( Column ) then
 result   := (( DefaultRowHeight - Column.Font.Size) div 2  - 3); ;
end;


procedure GridZebrado(Sender: TObject;
                      const Rect: TRect;
                      DataCol: Integer;
                      Column: TColumn;
                      State: TGridDrawState;
                      UserStyle  : TUserStyle);

   procedure AplicColorGrid( Sender: TObject; Grid: TCustomDBGrid ;
                            AFixedColor : TColor ;
                            ATitleFont : TFont);
   var
   i : integer;
   begin

   with  TDBGrid(Sender) do
   begin
   for i := 0 to pred(Columns.Count) do
   begin
    with  Columns.Items[i].Title.Font do
    begin
     Name     := ATitleFont.Name;
     Color    := ATitleFont.Color;
     Size     := ATitleFont.Size;
     Style    := ATitleFont.Style;
    end;
     TMyDBGrid(Columns.Items[i].Grid).FixedColor :=  AFixedColor;

   end;
   end;

   end;

 var
  FTitleFont : TFont;

 begin


   FTitleFont := TFont.Create;

   try

    with FTitleFont do
    begin
     Name := 'Tahoma';
     Color := TDBGrid(Sender).TitleFont.Color;
     Size := 8;
     Style := [];
    end;


  with TDBGrid(Sender) do
   begin

    if Assigned(Column.Grid) then
    begin

      AplicColorGrid ( Sender, Column.Grid , FixedColor , FTitleFont);

     if (gdSelected in State) then
      begin

      end
       else
        begin

        Canvas.Brush.Color :=  clHighlight;
        Canvas.Font.Style  := [];
        Canvas.Font.Color  :=  clNavy;

        if odd(DataSource.DataSet.RecNo)then
        Canvas.Brush.Color :=   UserStyle.GridColor.OddColor
        else
        Canvas.Brush.Color :=  UserStyle.GridColor.EvenColor;
        end;

        Canvas.FillRect(Rect);
        DefaultDrawColumnCell(rect, datacol, column, state);

     end;

     end;

     finally
      FreeAndNil( FTitleFont );
     end;

 end;


procedure GridCheckBox(ASender: TDBGrid; const Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
const
 CtrlState : array[Boolean] of Integer = (DFCS_BUTTONCHECK,DFCS_BUTTONCHECK or DFCS_CHECKED);
var
 R : TRect;
 FHandle: HDC;
begin

 if not Assigned(Column.Field) then
 exit;

  if (Column.Field.DataType = ftBoolean) then
    begin
      FHandle := ASender.Canvas.Handle;
      ASender.Canvas.FillRect(Rect);
      R.Left   := Rect.Left + 2;
      R.Right  := Rect.Right - 2;
      R.Top    := Rect.Top + 2;
      R.Bottom := Rect.Bottom - 2;
      DrawFrameControl(FHandle,R,DFC_BUTTON,CtrlState[Column.Field.AsBoolean]);
   end;

end;

end.
